/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.statusbar

import android.content.Context
import android.net.wifi.WifiInfo
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.controller.TcamWifiController
import com.ecarx.systemui.plugin.controller.WifiData
import com.ecarx.systemui.plugin.ui.custom.SystemBarButton
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager
import com.ecarx.systemui.plugin.utils.LogUtils
import ecarx.openapi.net.wifi.StaConnectionListener
import java.lang.Exception

class WifiView : SystemBarButton {

    companion object {
        private const val TAG = "WifiView"
    }

    constructor(context: Context?) : this(context!!, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(context!!, attrs, 0) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {

    }

    override fun getContentDescriptionString(): String {
        return "WifiView"
    }

    override fun initView() {
        super.initView()
        try {
            TcamWifiController.init(context)
            TcamWifiController.registerWifiConnectListener(object : TcamWifiController.ConnectionStatusListener {
                override fun onStateChanged(state: Int, wifiInfo: WifiInfo) {
                    showWifiIndicator(wifiInfo.rssi)
                }

                override fun onError(code: Int) {
                    LogUtils.d(TAG, "onError() called with: code = $code")
                }

            })
        } catch (exception: ClassNotFoundException) {
            exception.printStackTrace()
        } catch (throwable: NoClassDefFoundError) {
            throwable.printStackTrace()
        } catch (e: Exception){
            e.printStackTrace()
        }

        var wifiSignal = if (TcamWifiController.isWifiEnabled()) {
            val wifiData: WifiData? = TcamWifiController.getConnectionWifi()
            wifiData?.rssi ?: 0
        } else {
            -1
        }
        showWifiIndicator(wifiSignal)
        setOnClickListener { v: View? ->
            val locations = IntArray(2)
            getLocationOnScreen(locations)
            locations[0] = locations[0] - getPopupWindowWidth() / 2
            DialogManager.getDialogManager().showBtWifiDialog(context, locations, 2)
        }

    }

    private fun showWifiIndicator(signal: Int){
        setImageDrawable(
            ResourcesCompat
                .getDrawable(
                    resources,
                    getSignalResId(signal),
                    context.theme
                )
        )
    }

    private fun getPopupWindowWidth() : Int{
        return resources.getDimension(R.dimen.dp_710).toInt()
    }

    private fun getSignalResId(signal: Int): Int {
        if (signal == -1) {
            return R.drawable.ic_wifi_off
        }
        if (signal == 0) {
            return R.drawable.ic_wifi_0
        }
        if (signal == 1) {
            return R.drawable.ic_wifi_1
        }
        if (signal == 2) {
            return R.drawable.ic_wifi_2
        }
        return if (signal == 3) {
            R.drawable.ic_wifi_3
        } else {
            R.drawable.ic_wifi_4
        }
    }
}